#include "StdAfx.h"
#include "GrpPos.h"

#ifdef _DEBUG
#include "crtdbg.h"
#endif

CGrpPos::CGrpPos(void) : m_count(0)
{
  Clean();
}

void CGrpPos::Clean(void)
{
  memset(m_groups, 0, sizeof(m_groups));
  m_count = 0;
}

int CGrpPos::Add(wchar_t grp, int pos)
{
  int i=Add(grp);
  SetPosByIndex(i, pos);
  m_count = CalcCount();
  return i;
}

int CGrpPos::Add(wchar_t grp)
{
  int i;
  for (i=0;i<ALPHABET_MAX_LEN;i++)
  {
    if (m_groups[i]==grp || m_groups[i]==0)
      break;
  }

  m_groups[i]=grp;
  m_count = CalcCount();	// TODO: m_count = i ?

  return i;
}


int CGrpPos::GetGrpIndex(wchar_t grp) const
{
  int i;

  for (i=0;i<ALPHABET_MAX_LEN;i++)
  {
    if (m_groups[i]==grp)
      return i;
    if (m_groups[i]==0)
      break;
  }

  return -1;
}

int CGrpPos::GetGrpPos(wchar_t grp) const
{
  int idx = GetGrpIndex(grp);
  if (idx >= 0)
    return GetPosByIndex(idx);
  else
    return -1;
}

// TODO: use a member to save the count to improve performance
int CGrpPos::CalcCount() const
{
  int i;

  for (i=0;i<ALPHABET_MAX_LEN;i++)
  {
    if (m_groups[i]==0)
      break;
  }

  return i;
}
